﻿using System;
using System.Linq.Dynamic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Buffalo.Lib.Common;
using Ext.Net.MVC;
using Ext.Net;
using System.Web.Mvc;
using Buffalo.SMECommon.DAL.Models;
using System.Data.Linq;
using System.Collections;
using Buffalo.SMECommon.Enum;

namespace Buffalo.SMECommon.DAL
{
    public class PartnerDAL: BaseDAL
    {
        public Partner GetPartner(string IDPartner)
        {
            return GetEntity<Partner>(IDPartner);
        }
        public List<Partner> GetListPartner(int limit, int start, string dir, string sort, enuPartnerType partnerType, string criteria, string searchText, out int total)
        {
            string whereClause = string.Format("PartnerType = {0} or PartnerType = {1}", (int)partnerType, (int)enuPartnerType.SupplierCustomer);
            return GetListEntity<Partner>("gc_Partner", whereClause, limit, start, dir, sort, criteria, searchText, out total);
        }
        public bool SavePartners(string jsonData)
        {
            return SaveEntities<Partner>(jsonData, new string[] { "Code", "Name" });
        }
        public bool SaveCustomer(Partner customer)
        {
            //ttt   save for xxName
            //Industry industry = new BaseDAL().GetEntity<Industry>(customer.IDIndustry);
            return SaveEntity<Partner>(customer, new string [] {"Code", "Name"});
        }
        public bool SaveSupplier(Partner supplier)
        {
            //ttt   save for xxName
            //Industry industry = new BaseDAL().GetEntity<Industry>(supplier.IDIndustry);
            return SaveEntity<Partner>(supplier, new string[] { "Code", "Name" });
        }

        public bool LockSupplier(int monthNo, string IDSupplier, enuEntrySide entrySide, double amount)
        {
            //find Supplier
            Partner supplier = GetEntity<Partner>(IDSupplier);
            if (supplier == null)
                return false;

            //Tang ben No/Co
            string entrySideFieldName = string.Empty;
            double changeAmount = 0;
            if (entrySide == enuEntrySide.Debit)
            {
                entrySideFieldName = string.Format("Dr{0}", monthNo);
                changeAmount = amount;
            }
            else
            {
                entrySideFieldName = string.Format("Cr{0}", monthNo);
                changeAmount = -amount;
            }
            double curAmount = Util.ToSafeDouble(Util.GetValue(supplier, entrySideFieldName));
            Util.SetValue(supplier, entrySideFieldName, curAmount + amount);

            //Next Begin = Next Begin +/- $ 
            if (monthNo < 12)
            {
                string beginFieldName = string.Format("Begin{0}", monthNo + 1);
                double curBegin = Util.ToSafeDouble(Util.GetValue(supplier, beginFieldName));
                Util.SetValue(supplier, beginFieldName, curBegin + changeAmount);
            }
            //update
            return SaveEntity(supplier);
        }
        public bool LockCustomer(int monthNo, string IDCustomer, enuEntrySide entrySide, double amount)
        {
            //find Customer
            Partner customer = GetEntity<Partner>(IDCustomer);
            if (customer == null)
                return false;

            //Tang ben No/Co
            string entrySideFieldName = string.Empty;
            double changeAmount = 0;
            if (entrySide == enuEntrySide.Debit)
            {
                entrySideFieldName = string.Format("Dr{0}", monthNo);
                changeAmount = amount;
            }
            else
            {
                entrySideFieldName = string.Format("Cr{0}", monthNo);
                changeAmount = -amount;
            }
            double curAmount = Util.ToSafeDouble(Util.GetValue(customer, entrySideFieldName));
            Util.SetValue(customer, entrySideFieldName, curAmount + amount);

            //Next Begin = Next Begin +/- $ 
            if (monthNo < 12)
            {
                string beginFieldName = string.Format("Begin{0}", monthNo + 1);
                double curBegin = Util.ToSafeDouble(Util.GetValue(customer, beginFieldName));
                Util.SetValue(customer, beginFieldName, curBegin + changeAmount);
            }
            //update
            return SaveEntity(customer);
        }
        public bool UnLockSupplier(int monthNo, string IDSupplier, enuEntrySide entrySide, double amount)
        {
            //find Supplier
            Partner supplier = GetEntity<Partner>(IDSupplier);
            if (supplier == null)
                return false;

            //UnLock: Giam ben No/Co
            string entrySideFieldName = string.Empty;
            double changeAmount = 0;
            if (entrySide == enuEntrySide.Debit)
            {
                entrySideFieldName = string.Format("Dr{0}", monthNo);
                changeAmount = amount;
            }
            else
            {
                entrySideFieldName = string.Format("Cr{0}", monthNo);
                changeAmount = -amount;
            }
            double curAmount = Util.ToSafeDouble(Util.GetValue(supplier, entrySideFieldName));
            Util.SetValue(supplier, entrySideFieldName, curAmount - amount);

            //UnLock: Next Begin = Next Begin +/- $ 
            if (monthNo < 12)
            {
                string beginFieldName = string.Format("Begin{0}", monthNo + 1);
                double curBegin = Util.ToSafeDouble(Util.GetValue(supplier, beginFieldName));
                Util.SetValue(supplier, beginFieldName, curBegin - changeAmount);
            }
            //update
            return SaveEntity(supplier);
        }

    }
}
